package util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.StringTokenizer;

import topo.Edge;
import topo.Node;
import topo.Topology;


public class IOTopology {

	
	 public static Topology importTopologyFile(String path) throws NumberFormatException, IOException {
			
			HashMap<String, Node> hashmap = new HashMap<String, Node>();
			Topology topo = new Topology();
		
			
			BufferedReader reader=new BufferedReader(new FileReader(path) );
			//read first line which shows the name of company
			int numberOfContainers  = 0;
			String topologyType = null;
			
			topologyType = reader.readLine();
			if(topologyType == null){
				System.err.println("Error while reading the topology file");
				return null;
			}
			topo.setLabel(topologyType);
			
			numberOfContainers = Integer.parseInt(reader.readLine());
			if(numberOfContainers <= 0){
				System.err.println("Error while reading the topology file");
				return null;
			}
			Node aNode;
			String label;
			/// read containers' names
			for(int i=0;i<numberOfContainers ;i++){
				
				 aNode = new Node();
				 label= reader.readLine();
				 aNode.setLabel(Integer.parseInt(label) );
				 topo.addNode(aNode);
				 hashmap.put(label, aNode);
				
			}
			
			for (String line= reader.readLine();line !=null;line=reader.readLine()) {

				String temp = "";
				
				Node from;
				Node to;
				int cost = 0;
				Edge stf; 
								
				StringTokenizer tokens = new StringTokenizer(line);
				temp = tokens.nextToken();
				from = hashmap.get(temp);
							
				while(tokens.hasMoreTokens()){
					temp= tokens.nextToken();
					if(temp.equalsIgnoreCase(">"))
						continue;
					
					to = hashmap.get(temp);
					stf = new Edge(from,to);
					stf.setCost(cost);
					topo.addEdge(stf);
				}	
				
			
			}
			reader.close();
			
			return topo;
			
		

		}
	
	
//	public static void exportTopologyFile(GnutellaGraph g) {
//		
//		int numOfContainers = g.getNodes().length;
//		int containerIDs[]  = new int[numOfContainers];
//		
//		FileLogger topoloogyLogger = new FileLogger("topology.txt");
//		topoloogyLogger.setText("");
//		topoloogyLogger.log(g.getAttribut("topology")+"\n" );
//		topoloogyLogger.log(String.valueOf(numOfContainers) +"\n") ;
//		for (int i = 0; i < numOfContainers; i++) {
//			GnutellaNode gnode= g.getNodes()[i];
//			containerIDs[i] =   gnode.getId();
//			topoloogyLogger.log(containerIDs[i]+"\n");
//		}
//		
//		
//		for(int i=0;i< numOfContainers; i++){
//			GnutellaNode gnode= g.getNodes()[i];
//			GnutellaNode neighNodes[]=  g.connedtedNodes(gnode);
//			topoloogyLogger.log(containerIDs[i]+" >");
//			if(neighNodes != null)
//				for(int j=0;j<neighNodes.length;j++){
//					topoloogyLogger.log(" " + neighNodes[j].getId() );
//				}
//			topoloogyLogger.log("\n");
//		}	
//			
//	}
	
	public static void exportTopologyFile(Topology g,String filename) {
		
		int numOfContainers = g.getNodes().size();
		int containerIDs[]  = new int[numOfContainers];
		
		ResultFileWriter topoloogyLogger = new ResultFileWriter(filename);
		topoloogyLogger.setText("");
		topoloogyLogger.log(g.getLabel()+"\n" );
		topoloogyLogger.log(String.valueOf(numOfContainers) +"\n") ;
		for (int i = 0; i < numOfContainers; i++) {
			Node gnode= (Node) g.getNodes().get(i);
			containerIDs[i] =  gnode.getLabel() ;
			topoloogyLogger.log(containerIDs[i]+"\n");
		}
		
		
		for(int i=0;i< numOfContainers; i++){
			Node gnode= (Node) g.getNodes().get(i);
			Edge neighEdges[]=  g.getEdgesFromNode(gnode);
			topoloogyLogger.log(containerIDs[i]+" >");
			System.out.print(containerIDs[i]+" >");
			if(neighEdges != null)
				for(int j=0;j<neighEdges.length;j++){
					topoloogyLogger.log(" " +neighEdges[j].getNodeOtherThan( gnode ).getLabel() );
					System.out.print(" " + neighEdges[j].getNodeOtherThan( gnode ).getLabel() );
				}
			neighEdges =  g.getEdgesToNode(gnode);
			if(neighEdges != null)
				for(int j=0;j<neighEdges.length;j++){
					topoloogyLogger.log(" " +neighEdges[j].getNodeOtherThan( gnode ).getLabel() );
					System.out.print(" " + neighEdges[j].getNodeOtherThan( gnode ).getLabel() );
				}
			topoloogyLogger.log("\n");
			System.out.println();
		}	
			
	}
	
	
}
